<template>
    <div id="monthWork">
      <el-upload
          class="upload-demo"
          action="https://jsonplaceholder.typicode.com/posts/"
          :on-preview="handlePreview"
          :before-upload="beforeAvatarUpload"
          :on-remove="handleRemove"
          multiple
          :limit="1"
          :on-exceed="handleExceed"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传xls、xlsx文件，且不超过10MB</div>
        </el-upload>
    </div>
</template>
<script>
export default {
  name: 'monthWork',
  data () {
    return {
      fileList: []
    }
  },
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    beforeAvatarUpload (file) {
        var fileName=new Array()
        fileName =file.name.split('.');
        const extension = fileName[fileName.length-1] === 'xls'
        const extension2 =  fileName[fileName.length-1]=== 'xlsx'
        const isLt2M = file.size / 1024 / 1024 < 10
        if (!extension && !extension2) {
            this.$message({
                message: '上传模板只能是xls、xlsx格式!',
                type: 'warning'
            });
            //console.log('上传模板只能是xls、xlsx格式!')
        }
        if (!isLt2M) {
            this.$message({
                message: '上传模板大小不能超过 10MB!',
                type: 'warning'
            });
            //console.log('上传模板大小不能超过 10MB!')
        }
        if (extension || extension2 && isLt2M == true) {
            console.log(file)
            let fd = new FormData()
            fd.append('invoiceTypeId', this.invoice_type_id)//随文件上传的其他参数
            fd.append('epid', this.epid)
            fd.append('file', file)
            console.log(fd)
            this.newImport(fd).then(function (res) {//校验完成后提交
                console.log(res)
            }, function () {
                console.log('failed');
            });
            return true
        }
        return extension || extension2 && isLt2M
    },
    newImport (data) {
        var self = this;
        self.getRequest(
          "/pm/access/code/query/" +
          self.accessCode +
          "?page_no=" +
          2, {}
        ).then(res => {
          console.log('success');
          console.log(res);
        },function(){
          console.log('failed');
        });
    }
  }
}
</script>

<style scoped>

</style>